package controller;

import java.util.Calendar;

import com.jfinal.core.Controller;

import config.model.User;
import service.AnnouncementService;
import service.MarkService;
import service.ProjectService;

/**
 * 评委面控制类
 * 
 * @author 陈凌志
 *
 */

public class TeacherController extends Controller{
	
	AnnouncementService as = new AnnouncementService();
	ProjectService ps = new ProjectService();
	MarkService ms = new MarkService();


	/**
	 * 访问评委主页面方法
	 * 
	 */
	public void index() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		setSessionAttr("thisYear", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		setAttr("title","公告信息");
		setAttr("announcementPage",as.queryAnnouncementPage(pageNumber));
		render("index.html");
	}
	
	/**
	 * 此方法用于评分页面访问
	 * 
	 */
	public void  mark() {
		User u = getSessionAttr("User");
		String group = u.getPower();
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String projectYear = getPara("projectYear");
		setAttr("projectPage",ps.queryProjectByGroup(group,pageNumber,projectYear));
		setAttr("mark",ms.queryMark());
		setAttr("file",ps.getFileName(ps.queryProjectByGroup(group,pageNumber,projectYear).getList(),"application"));
		setAttr("year",ps.getProjectYear());
		setAttr("selectYear",projectYear);
		setAttr("title","评分");
		render("mark.html");
	}
	
	/**
	 * 此方法用于评委评分
	 * 
	 */
	public void  addMark() {
		User u = getSessionAttr("User");
		String teacher = u.getName();
		MarkService mk = new MarkService();
		String key = "评分成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				String mark = getPara(a[i]+"mark");
				int PID =  Integer.parseInt(getPara(a[i]+"PID"));
				if(!mark.isEmpty()) {
					if(mk.addMark(mark, a[i], teacher,PID)) {
						
					}else {
						key = "某个没有成功";
					}
				}else {
					key = "某个没有填写分数，填写过的成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何项目");
			render("success.html");
		}
		
		
	}
	

	/**
	 * 退出登录方法
	 * 
	 */
	public void signOut() {
		removeSessionAttr("User");
		redirect("/index");
	}
	
}
